//
//  ZJSIPotoView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJSIPotoView.h"

@interface ZJSIPotoView ()

@end

@implementation ZJSIPotoView

- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame title:title];
    if (self) {
        
    }
    return self;
}

- (void)updateUI_PotoArray:(NSArray *)potoArray{
    self.potoImageArray = [potoArray mutableCopy];
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view removeFromSuperview];
        }
    }
    CGFloat maxHeight = 0;
    
    CGFloat top = self.titleLabel.bottom;
    CGFloat left = 12 * [UIScreen zj_screenWScale];
    CGFloat space = left;
    CGFloat imageW = (self.width - 6 * space) / 5;
    for (int i = 0; i < potoArray.count + 1; i++) {
        NSInteger yu = i % 5;
        NSInteger zheng = i / 5;
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(left + yu * (imageW + space), (imageW + space) * zheng + top, imageW, imageW)];
        button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor zj_colorWithHexString:@"ebebeb"].CGColor;
        button.tag = i;
        
       
        if (i == potoArray.count) {
            [button setImage:[UIImage imageNamed:@"btn_addimg"] forState:UIControlStateNormal];
        }else{
            NetWorkUploadFileModel *model = potoArray[i];
            [button setImage:[UIImage imageWithData:model.FILE_PATH] forState:UIControlStateNormal];
        }
        maxHeight = button.bottom;
         [button addTarget:self action:@selector(action_imageButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        if (i != potoArray.count) {
            ///deleButton
            UIButton *deleButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, imageW / 3, imageW / 3)];
            deleButton.center = CGPointMake(button.right, button.y);
            deleButton.tag = i;
            [deleButton setImage:[UIImage imageNamed:@"voice_close"] forState:UIControlStateNormal];
            [deleButton addTarget:self action:@selector(action_deleButton:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:deleButton];
 
        }
        
    }
    
    self.height = maxHeight + 6 * [UIScreen zj_screenHScale];
}


#pragma mark - 点击事件
- (void)action_imageButtonClick:(UIButton *)button{
    if (button.tag == self.potoImageArray.count) {//点击加号
      
        if (self.deletate) {
            [self.deletate ZJSIPotoViewDelegate_addPotoClick];
        }
    }else{
        if (self.deletate) {
            [self.deletate ZJSIPotoViewDelegate_potoImageClick_index:button.tag];
        }
    }

}
//删除
- (void)action_deleButton:(UIButton *)button{
    if (self.deletate) {
        [self.deletate ZJSIPotoViewDelegate_deletePotoClick_index:button.tag];
    }
}

- (NSMutableArray *)potoImageArray{
    if (_potoImageArray == nil){
        _potoImageArray = [NSMutableArray array];
        
    }
    return _potoImageArray;
}
@end
